1
|
|
|
// spec/Error.spec.js |
2
|
|
View Code Duplication |
"use strict"; |
|
|
|
|
3
|
|
|
|
4
|
|
|
// :: DEPENDENCIES |
5
|
|
|
|
6
|
|
|
// load native dependencies |
7
|
|
|
const path = require("path"); |
8
|
|
|
|
9
|
|
|
// load local dependencies |
10
|
|
|
const root = path.dirname(__dirname); |
11
|
|
|
const Throwable = require(path.join(root, "Throwable.js")); |
12
|
|
|
const Error2 = require(path.join(root, "Error.js")); |
13
|
|
|
|
14
|
|
|
// :: TESTING |
15
|
|
|
|
16
|
|
|
// test the Error 'class' |
17
|
|
|
describe("Error", () => { |
18
|
|
|
|
19
|
|
|
// :: INHERITED PROTOTYPE |
20
|
|
|
|
21
|
|
|
it("should inherit from 'Object'", () => { |
22
|
|
|
expect(new Error2()).toEqual(jasmine.any(Object)); |
23
|
|
|
}); |
24
|
|
|
|
25
|
|
|
it("should inherit from 'Throwable'", () => { |
26
|
|
|
expect(new Error2()).toEqual(jasmine.any(Throwable)); |
27
|
|
|
}); |
28
|
|
|
|
29
|
|
|
it("should have a prototype method named 'toString()'", () => { |
30
|
|
|
expect(Error2.prototype).toHaveMethod("toString"); |
31
|
|
|
}); |
32
|
|
|
|
33
|
|
|
it("should have a prototype method named 'native()'", () => { |
34
|
|
|
expect(Error2.prototype).toHaveMethod("native"); |
35
|
|
|
}); |
36
|
|
|
|
37
|
|
|
it("should have a prototype property string named 'name'", () => { |
38
|
|
|
expect(Error2.prototype).toHaveString("name"); |
39
|
|
|
}); |
40
|
|
|
|
41
|
|
|
it("should have a prototype property string named 'message'", () => { |
42
|
|
|
expect(Error2.prototype).toHaveString("message"); |
43
|
|
|
}); |
44
|
|
|
|
45
|
|
|
it("should have a prototype property string named 'code'", () => { |
46
|
|
|
expect(Error2.prototype).toHaveMember("code"); |
47
|
|
|
}); |
48
|
|
|
|
49
|
|
|
// :: EXTENDED PROTOTYPE |
50
|
|
|
|
51
|
|
|
// :: PROTOTYPE VALUES |
52
|
|
|
|
53
|
|
|
it("should have the 'class' name in the prototype property named 'name'", () => { |
54
|
|
|
expect(Error2.prototype.name).toEqual("Error"); |
55
|
|
|
}); |
56
|
|
|
|
57
|
|
|
it("should have a dummy default value as message", () => { |
58
|
|
|
expect(Error2.prototype.message).toEqual("thrown"); |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
it("should have a null default value as code", () => { |
62
|
|
|
expect(Error2.prototype.code).toBeNull(); |
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
// :: CONSTRUCTOR |
66
|
|
|
|
67
|
|
|
it("should instantiate without parameters", () => { |
68
|
|
|
let arg1, arg2, arg3, test; |
69
|
|
|
test = (() => new Error2(arg1, arg2, arg3)); |
70
|
|
|
for (let i = 0; i < 2; i += 1) { |
71
|
|
|
for (let j = 0; j < 2; j += 1) { |
72
|
|
|
for (let e = 0; e < 2; e += 1) { |
73
|
|
|
arg1 = (i % 2 === 0 ? undefined : null); |
74
|
|
|
arg2 = (j % 2 === 0 ? undefined : null); |
75
|
|
|
arg3 = (e % 2 === 0 ? undefined : null); |
76
|
|
|
expect(test).not.toThrowError("parameter 'name' must be a 'string'"); |
77
|
|
|
expect(test).not.toThrowError("parameter 'message' must be a 'string'"); |
78
|
|
|
expect(test).not.toThrowError("parameter 'code' must be a 'number'"); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
test = (() => new Error2()); |
83
|
|
|
expect(test).not.toThrowError("parameter 'name' must be a 'string'"); |
84
|
|
|
expect(test).not.toThrowError("parameter 'message' must be a 'string'"); |
85
|
|
|
expect(test).not.toThrowError("parameter 'code' must be a 'number'"); |
86
|
|
|
}); |
87
|
|
|
|
88
|
|
|
it("should instantiate with parameters", () => { |
89
|
|
|
let arg1, arg2, arg3, test1, test2, test3; |
90
|
|
|
const args1 = [undefined, null, "Error"]; |
91
|
|
|
const args2 = [undefined, null, "thrown"]; |
92
|
|
|
const args3 = [undefined, null, Math.round(Math.random() * 0xFFFFFFFF)]; |
93
|
|
|
test1 = (() => new Error2(arg1)); |
94
|
|
|
test2 = (() => new Error2(arg1, arg2)); |
95
|
|
|
test3 = (() => new Error2(arg1, arg2, arg3)); |
96
|
|
|
for (let i = 0; i < args1.length; i += 1) { |
97
|
|
|
arg1 = args1[i]; |
98
|
|
|
for (let j = 0; j < args2.length; j += 1) { |
99
|
|
|
arg2 = args2[j]; |
100
|
|
|
for (let e = 0; e < args3.length; e += 1) { |
101
|
|
|
arg3 = args3[e]; |
102
|
|
|
expect(test3).not.toThrowError("parameter 'name' must be a 'string'"); |
103
|
|
|
expect(test3).not.toThrowError("parameter 'message' must be a 'string'"); |
104
|
|
|
expect(test3).not.toThrowError("parameter 'code' must be a 'number'"); |
105
|
|
|
} |
106
|
|
|
expect(test2).not.toThrowError("parameter 'name' must be a 'string'"); |
107
|
|
|
expect(test2).not.toThrowError("parameter 'message' must be a 'string'"); |
108
|
|
|
expect(test2).not.toThrowError("parameter 'code' must be a 'number'"); |
109
|
|
|
} |
110
|
|
|
expect(test1).not.toThrowError("parameter 'name' must be a 'string'"); |
111
|
|
|
expect(test1).not.toThrowError("parameter 'message' must be a 'string'"); |
112
|
|
|
expect(test1).not.toThrowError("parameter 'code' must be a 'number'"); |
113
|
|
|
} |
114
|
|
|
}); |
115
|
|
|
|
116
|
|
|
it("should throw an Error if 'name', 'message' or 'code' are invalid parameters", () => { |
117
|
|
|
let arg1, arg2, arg3, test31, test32, test33, test21, test22, test11; |
118
|
|
|
const noStr = [{}, true, false, 42, 3.1416, -42, -3.1416, () => null]; |
119
|
|
|
const noNmb = [{}, true, false, "", "qwerty", () => null]; |
120
|
|
|
test33 = (() => new Error2(arg1, arg2, arg3)); |
121
|
|
|
test32 = (() => new Error2(null, arg2, arg3)); |
122
|
|
|
test31 = (() => new Error2(null, null, arg3)); |
123
|
|
|
test22 = (() => new Error2(arg1, arg2)); |
124
|
|
|
test21 = (() => new Error2(null, arg2)); |
125
|
|
|
test11 = (() => new Error2(arg1)); |
126
|
|
|
if (typeof Symbol === "function") { |
127
|
|
|
noStr.push(Symbol("symbol")); |
128
|
|
|
noNmb.push(Symbol("symbol")); |
129
|
|
|
} |
130
|
|
|
for (let i = 0; i < noStr.length; i += 1) { |
131
|
|
|
arg1 = noStr[i]; |
132
|
|
|
for (let j = 0; j < noStr.length; j += 1) { |
133
|
|
|
arg2 = noStr[j]; |
134
|
|
|
for (let e = 0; e < noNmb.length; e += 1) { |
135
|
|
|
arg3 = noNmb[e]; |
136
|
|
|
expect(test33).toThrowError("parameter 'name' must be a 'string'"); |
137
|
|
|
expect(test32).toThrowError("parameter 'message' must be a 'string'"); |
138
|
|
|
expect(test31).toThrowError("parameter 'code' must be a 'number'"); |
139
|
|
|
} |
140
|
|
|
expect(test22).toThrowError("parameter 'name' must be a 'string'"); |
141
|
|
|
expect(test21).toThrowError("parameter 'message' must be a 'string'"); |
142
|
|
|
} |
143
|
|
|
expect(test11).toThrowError("parameter 'name' must be a 'string'"); |
144
|
|
|
} |
145
|
|
|
}); |
146
|
|
|
|
147
|
|
|
// :: MEMBER PROPERTIES |
148
|
|
|
|
149
|
|
|
it("should have all correct properties once instantiated", () => { |
150
|
|
|
const name = "qwerty"; |
151
|
|
|
const message = "asdf"; |
152
|
|
|
const code = Math.round(Math.random() * 0xFFFFFFFF); |
153
|
|
|
for (let i = 0; i < 2; i += 1) { |
154
|
|
|
const arg1 = (i % 2 === 0 ? name : null); |
155
|
|
|
const source1 = new Error2(arg1); |
156
|
|
|
for (let j = 0; j < 2; j += 1) { |
157
|
|
|
const arg2 = (j % 2 === 0 ? message : null); |
158
|
|
|
const source2 = new Error2(arg1, arg2); |
159
|
|
|
for (let e = 0; e < 2; e += 1) { |
160
|
|
|
const arg3 = (e % 2 === 0 ? code : null); |
161
|
|
|
const source3 = new Error2(arg1, arg2, arg3); |
162
|
|
|
if (i % 2 === 0) { |
163
|
|
|
expect(source1.name).toEqual(name); |
164
|
|
|
expect(source2.name).toEqual(name); |
165
|
|
|
expect(source3.name).toEqual(name); |
166
|
|
|
} else { |
167
|
|
|
expect(source1.name).toEqual(Error2.prototype.name); |
168
|
|
|
expect(source2.name).toEqual(Error2.prototype.name); |
169
|
|
|
expect(source3.name).toEqual(Error2.prototype.name); |
170
|
|
|
} |
171
|
|
|
if (j % 2 === 0) { |
172
|
|
|
expect(source2.message).toEqual(message); |
173
|
|
|
expect(source3.message).toEqual(message); |
174
|
|
|
} else { |
175
|
|
|
expect(source2.message).toEqual(Error2.prototype.message); |
176
|
|
|
expect(source3.message).toEqual(Error2.prototype.message); |
177
|
|
|
} |
178
|
|
|
if (e % 2 === 0) { |
179
|
|
|
expect(source3.code).toEqual(code); |
180
|
|
|
} else { |
181
|
|
|
expect(source3.code).toBeNull(); |
182
|
|
|
} |
183
|
|
|
expect(source1.message).toEqual(Error2.prototype.message); |
184
|
|
|
expect(source1.code).toBeNull(); |
185
|
|
|
expect(source2.code).toBeNull(); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
}); |
190
|
|
|
|
191
|
|
|
// :: MEMBER METHODS |
192
|
|
|
|
193
|
|
|
const name = "qwerty"; |
194
|
|
|
const message = "asdf"; |
195
|
|
|
const code = Math.round(Math.random() * 0xFFFFFFFF); |
196
|
|
|
|
197
|
|
|
it("#toString()", () => { |
198
|
|
|
// test non-empty |
199
|
|
|
for (let i = 0; i < 2; i += 1) { |
200
|
|
|
const arg1 = (i % 2 === 0 ? name : null); |
201
|
|
|
const source1 = new Error2(arg1); |
202
|
|
|
for (let j = 0; j < 2; j += 1) { |
203
|
|
|
const arg2 = (j % 2 === 0 ? message : null); |
204
|
|
|
const source2 = new Error2(arg1, arg2); |
205
|
|
|
for (let e = 0; e < 2; e += 1) { |
206
|
|
|
const arg3 = (e % 2 === 0 ? code : null); |
207
|
|
|
const source3 = new Error2(arg1, arg2, arg3); |
208
|
|
|
const str1 = source1.toString(); |
209
|
|
|
const str2 = source2.toString(); |
210
|
|
|
const str3 = source3.toString(); |
211
|
|
|
let exp1, exp2, exp3; |
212
|
|
|
if (i % 2 === 0) { |
213
|
|
|
exp1 = name + ": " + Error2.prototype.message + '.'; |
214
|
|
|
exp2 = exp3 = name; |
215
|
|
|
} else { |
216
|
|
|
exp1 = Error2.prototype.name + ": " + Error2.prototype.message + '.'; |
217
|
|
|
exp2 = exp3 = Error2.prototype.name; |
218
|
|
|
} |
219
|
|
|
if (e % 2 === 0) { |
220
|
|
|
exp3 += " (0x" + code.toString(16) + ')'; |
221
|
|
|
} |
222
|
|
|
if (j % 2 === 0) { |
223
|
|
|
exp2 += ": " + message + '.'; |
224
|
|
|
exp3 += ": " + message + '.'; |
225
|
|
|
} else { |
226
|
|
|
exp2 += ": " + Error2.prototype.message + '.'; |
227
|
|
|
exp3 += ": " + Error2.prototype.message + '.'; |
228
|
|
|
} |
229
|
|
|
expect(str1).toEqual(exp1); |
230
|
|
|
expect(str2).toEqual(exp2); |
231
|
|
|
expect(str3).toEqual(exp3); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
}); |
236
|
|
|
|
237
|
|
|
it("#native()", () => { |
238
|
|
|
// test non-empty |
239
|
|
|
for (let i = 0; i < 2; i += 1) { |
240
|
|
|
const arg1 = (i % 2 === 0 ? name : null); |
241
|
|
|
const source1 = new Error2(arg1); |
242
|
|
|
for (let j = 0; j < 2; j += 1) { |
243
|
|
|
const arg2 = (j % 2 === 0 ? message : null); |
244
|
|
|
const source2 = new Error2(arg1, arg2); |
245
|
|
|
for (let e = 0; e < 2; e += 1) { |
246
|
|
|
const arg3 = (e % 2 === 0 ? code : null); |
247
|
|
|
const source3 = new Error2(arg1, arg2, arg3); |
248
|
|
|
const err1 = source1.native(); |
249
|
|
|
const err2 = source2.native(); |
250
|
|
|
const err3 = source3.native(); |
251
|
|
|
const exp1 = Error2.prototype.message; |
252
|
|
|
const exp2 = (j % 2 === 0 ? message : Error2.prototype.message); |
253
|
|
|
const exp3 = exp2; |
254
|
|
|
expect(err1).toEqual(new Error(exp1)); |
255
|
|
|
expect(err2).toEqual(new Error(exp2)); |
256
|
|
|
expect(err3).toEqual(new Error(exp3)); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
}); |
261
|
|
|
|
262
|
|
|
}); |